package Tree;

import java.util.ArrayList;
import java.util.List;

public class _102_BinaryTreeLevelOrderTraversal {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res = new ArrayList<>();
        if(root == null) return res;
        List<TreeNode> tNodes = new ArrayList<>();
        tNodes.add(root);
        while (!tNodes.isEmpty()) {
            List<TreeNode> tn = new ArrayList<>();
            List<Integer> tr = new ArrayList<>();
            for (TreeNode node : tNodes) {
                if (node.left != null) {
                    tn.add(node.left);
                }
                if (node.right != null) {
                    tn.add(node.right);
                }
                tr.add(node.val);
                tNodes = tn;
            }
            res.add(tr);
        }
        return res;
    }
}
